import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nmb/services/global.dart';
import 'package:timeago/timeago.dart' as timeago show format;

import '../models/fold.dart';
import '../models/note.dart';
import 'note_detail.dart';

class ListNoteByFold extends StatefulWidget {
  const ListNoteByFold({super.key, required this.fold});
  final Fold fold;
  @override
  State<ListNoteByFold> createState() => _ListNoteByFoldState();
}

class _ListNoteByFoldState extends State<ListNoteByFold> {

  List<Note> list = [];

  final service = Get.find<GlobalService>();

  @override
  void initState() {
    super.initState();
    if(widget.fold.id.isNotEmpty){
      list = service.noteList.where((e)=>e.foldId==widget.fold.id).toList();
    }else{
      list = service.noteList;
    }
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.fold.name),
      ),
      body: ListView.builder(
          itemCount: list.length,
          itemBuilder: (context,index){
            final note = list[index];
            return Container(
              margin: EdgeInsets.only(left: 16.0,right: 16,bottom: 16),
              decoration: BoxDecoration(
                  color: Color(0xff242424),
                  borderRadius: BorderRadius.circular(16.0)
              ),
              child: ListTile(
                title: Text(note.title,style: TextStyle(
                    color: Colors.white,
                    fontSize: 22,
                    fontWeight: FontWeight.bold
                ),),
                subtitle: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      note.content.length > 36
                          ? '${note.content.substring(0, 36)}...'
                          : note.content,
                      style: TextStyle(
                          fontSize: 16,
                          color: Color(0xff939393)
                      ),
                    ),
                    Text(
                      note.dateModified != null
                          ? timeago.format(note.dateModified!,locale: 'zh_CN')
                          : timeago.format(note.dateCreated,locale: 'zh_CN'),
                      style: const TextStyle(fontSize: 14,color: Color(0xff939393)),
                    ),
                  ],
                ),
                onTap: () {
                  Get.to(NoteDetailScreen(noteId: note.id!));
                },
              ),
            );
      }),
    );
  }
}

